<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\BanUserForm;
use App\Admin\Actions\BatchEcpmFilter;
use App\Admin\Actions\EcpmFilter;
use App\Admin\Actions\SetParentForm;
use App\Constant\Enum;
use App\Models\User;
use App\Services\Wallet\UserWalletService;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Show;

class UserController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new User(), function (Grid $grid) {
            $grid->quickSearch('id', 'uuid', 'nickname', 'parent_id', 'phone', 'cid')->placeholder('输入 id\uuid\用户昵称\上级id\手机号\渠道')->auto(false);;
            $grid->selector(function (Grid\Tools\Selector $selector) {
                $selector->select('status', '用户状态', Enum::getStatusExplains());
                $selector->select('is_show_ad', '启用广告', [
                    1 => '是', 0 => '否'
                ]);
                $selector->select('is_group_leader', '是否团长', [
                    1 => '是', 0 => '否'
                ]);

            });

            $grid->model()->with(['wallet', 'parent', 'member', 'userwallet', 'member.leader:id,uuid,nickname']);
            $grid->model()->orderBy('created_at', 'desc');

            $grid->column('id')->sortable();
            $grid->column('uuid');
            // $grid->column('openid');
            $grid->column('nickname');
            $grid->column('phone');
            $grid->column('avatar')->image('', 45, 45);
            $grid->column('wallet.reward_coins', '金币账户')->display(function () {
                $html = <<<HTML
<p style="padding: 0; margin: 0"><span style="font-size: 10px; color: grey">全部 </span><span style="color: blue">%s</span></p>
<p style="padding: 0; margin: 0"><span style="font-size: 10px; color: grey">提现中 </span><span style="color: red">%s</span></p>
HTML;
                $withdrawing = UserWalletService::getUserWithdrawCoins($this);
                return sprintf($html, UserWalletService::getUserAllCoins($this), $withdrawing);
            })->sortable();
            $grid->column('userwallet.reward_coins', '累计提现')->display(function () {
                $html = <<<HTML
<p style="padding: 0; margin: 0"><span style="font-size: 10px; color: grey">全部 </span><span style="color: blue">%s</span></p>

HTML;
                $withdrasum = UserWalletService::getUserWithdrawsum($this);

                return sprintf($html, $withdrasum);
            });
            $grid->column('parent_id', '来源(上级)')
                ->width('200')
                ->display(function () {
                    $parent = $this->parent;
                    $leader = $this->member->leader ?? '';

                    $text = '<i>-</i>';
                    if ($parent) {
                        $text = "师傅: <a href='?id={$parent->id}'>{$parent->nickname}｜<b>{$parent->uuid}</b></a>";
                    }
                    if ($leader) {
                        $text .= "<br/> 团长: <a href='?id={$leader->id}'>{$leader->nickname}｜<b>{$leader->uuid}</a>";
                    }
                    return "<div style='font-size: smaller'>{$text}</div>";
                });


            $grid->column('setParent', '设置上级')
                ->display('设置')
                ->modal(function () {
                    /** @var User $model */
                    $model = $this;

                    return SetParentForm::make([
                        'user_id' => $model->getAttribute('id'),
                    ]);
                });
            $grid->column('children', '下级')
                ->display(function () {
                    $count = $this->children()->count();
                    return "<a href='?parent_id={$this->id}'><i class='fa fa-group'></i> {$count}</a>";
                });
            $grid->column('is_group_leader')
                ->using([
                    1 => '是',
                    0 => '否'
                ])
                ->label([
                    1 => 'success',
                    0 => 'default',
                ]);
            $grid->column('status')->using([
                Enum::STATUS_ENABLED => '已启用',
                Enum::STATUS_BLOCKED => '已封禁',
                Enum::STATUS_DISABLED => '已禁用',
            ])->label([
                Enum::STATUS_ENABLED => 'success',
                Enum::STATUS_BLOCKED => 'warning',
                Enum::STATUS_DISABLED => 'danger',
            ])->sortable();
            $grid->column('is_show_ad')
                ->using([
                    1 => '是',
                    0 => '否'
                ])
                ->label([
                    1 => 'success',
                    0 => 'default',
                ]);
            $grid->column('cid');
            $grid->column('registered_at')->sortable();
            $grid->column('last_login_at')->sortable();
            $grid->column('last_login_ip');

            $grid->disableBatchDelete();
            $grid->disableCreateButton();
            $grid->disableDeleteButton();
            $grid->disableQuickEditButton();
            $grid->disableEditButton();
            $grid->showColumnSelector();
            $grid->setActionClass(Grid\Displayers\Actions::class);


            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
                $filter->equal('parent_id');
                $filter->like('uuid');
                $filter->like('nickname');
                $filter->like('phone');
                $filter->like('cid');

                $filter->equal('is_group_leader')->radio([
                    1 => '是',
                    0 => '否'
                ]);

                $filter->equal('is_show_ad')->radio([
                    1 => '是',
                    0 => '否'
                ]);

                $filter->equal('status')->radio([
                    Enum::STATUS_ENABLED => '已启用',
                    Enum::STATUS_BLOCKED => '已封禁',
                ]);
            });


            $grid->tools(function ($tools) {
                $tools->batch(function ($batch) {
                    $batch->add(new BatchEcpmFilter('禁用广告', 0));
                    $batch->add(new BatchEcpmFilter('启用广告', 1));
                });
            });

            $grid->column('remark', '备注')->limit(10);
            $grid->column('action', '管理')
                ->display('')
                ->modal(function () {
                    /** @var User $model */
                    $model = $this;

                    return BanUserForm::make([
                        'user_id' => $model->getAttribute('id'),
                        'name' => $model->getAttribute('nickname'),
                    ]);
                });

            $grid->fixColumns(4);
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new User(), function (Show $show) {
            $show->disableDeleteButton();
            $show->disableEditButton();

            $show->field('id');
            $show->field('parent_id');
            $show->field('uuid');
            $show->field('username');
            $show->field('password');
            $show->field('phone');
            $show->field('nickname');
            $show->field('avatar');
            $show->field('status');
            $show->field('openid');
            $show->field('cid');
            $show->field('registered_at');
            $show->field('last_login_at');
            $show->field('last_login_ip');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new User(), function (Form $form) {
            $form->display('id');
            $form->text('parent_id');
            $form->text('uuid');
            $form->text('username');
            $form->text('password');
            $form->text('phone');
            $form->text('nickname');
            $form->text('avatar');
            $form->text('status');
            $form->text('openid');
            $form->text('registered_at');
            $form->text('last_login_at');
            $form->text('last_login_ip');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
